package com.freewater.springboot.demo.aop.advice.generic;

import java.util.Collection;
import java.util.Objects;
import java.util.stream.Collectors;

public class PersonService implements IService<Person> {

    /**
     * 新增人员
     */
    public Person addPerson(Person person) {
        System.out.printf("person added：%s%n", person);
        return person;
    }

    /**
     * 新增人员
     */
    public Person addPerson(String name, int age) {
        if (name == null) {
            throw new NullPointerException("name can not be null");
        }
        if (age < 0) {
            throw new NullPointerException("age can not be negative");
        }
        Person person = new Person();
        person.setName(name);
        person.setAge(age);
        System.out.printf("person added：%s%n", person);
        return person;
    }

    @Override
    public String describe(Person person) {
        return person.toString();
    }

    @Override
    public String describeCollection(Collection<Person> persons) {
        return persons.stream()
                .filter(Objects::nonNull).map(Person::toString)
                .collect(Collectors.joining(","));
    }
}
